<?php
namespace catchAdmin\mbqy\controller;

use catcher\CatchResponse;
use app\common\utils\Upfile;
use catcher\base\CatchRequest;
use catcher\base\CatchController;
use app\common\model\ServicePackage as ServicePackageModel;
use app\common\utils\Uniquecode;
use app\common\lib\Cachedb;
use app\common\lib\ZhyxMongo;

class Qyfw extends CatchController
{
    protected $servicePackage;

    public function __construct(ServicePackageModel $servicePackage)
    {
        $this->servicePackage = $servicePackage;
    }

    /**
     * 列表
     *
     * @time 2020年01月09日
     * @param CatchRequest $request
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function index(): \think\response\Json
    {
        return CatchResponse::paginate($this->servicePackage->getList());
    }

    /**
     * 保存
     *
     * @time 2020年01月09日
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function save(CatchRequest $request): \think\response\Json
    {
        $param = $request->param();
        $param['unique_no'] = uniqid('FWB');
        return CatchResponse::success($this->servicePackage->addData($param));
    }

    /**
     * 更新
     *
     * @time 2020年01月09日
     * @param $id
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function update($id, CatchRequest $request): \think\response\Json
    {
        $pack = (new ServicePackageModel())->where('id', $id)->field('id,unique_no,contentList')->find()->toArray();

        if (!empty($pack['contentList'])) {
            foreach ($pack['contentList'] as $key => $val) {
                $pack['contentList'][$key]['parent_id'] = $pack['id'];
            }

            ZhyxMongo::getInstance()->setCollection('service_bind')->updateMany(['children.unique_no' => $pack['unique_no']],['$pull' => ['children.$.children' => ['$exists' => true]]]);

            ZhyxMongo::getInstance()->setCollection('service_bind')->updateMany(['children.unique_no' => $pack['unique_no']],['$push' => ['children.$.children' => ['$each' => $pack['contentList']]]]);
        }

        return CatchResponse::success($this->servicePackage->saveData($id, $request->param()));
    }

    /**
     * 删除
     *
     * @time 2020年01月09日
     * @param $id
     * @return \think\response\Json
     */
    public function delete($id): \think\response\Json
    {
        return CatchResponse::success($this->servicePackage->destroy($id));
    }

    /**
     * 上传文件
     *
     * @param CatchRequest $request
     * @return \think\response\Json
     */    
    public function uploadfile(CatchRequest $request): \think\response\Json
    {
        return CatchResponse::success((new Upfile)->upLoad($request->file(), 1));
    }

    /**
     * 获取全部数据
     * @return \think\response\Json
     */
    public function getfwpackage(): \think\response\Json
    {
        return CatchResponse::success($this->servicePackage->getRows());
    }

    // public function test()
    // {
    //     return CatchResponse::success((new Cachedb)->getCachedb());
    // }
}